package com.chixing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.ServerResponse;
import com.chixing.pojo.Cart;
import com.chixing.pojo.CustomerTokenDTO;
import com.chixing.pojo.Product;
import com.chixing.pojo.Users;
import com.chixing.service.ICartService;
import com.chixing.service.IProductService;
import com.chixing.service.IUsersService;
import com.chixing.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyc
 * @since 2022-10-21
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IUsersService usersService;

    @GetMapping("shopcart")
    @ResponseBody
    public ServerResponse loadcartData(HttpServletRequest request)
    {
        String token = request.getHeader("token");
        System.out.println("token:"+token);

        CustomerTokenDTO user2 = TokenUtil.parseToken(token);
        Integer cartNum = 0;

        Users user = new Users();
        user = usersService.selectById(user2.getCustId());

        if (token!=null){
//            user = TokenUtil.parseToken(token);
            cartNum = cartService.getUserCartProductCount(user.getUserId());
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",user.getUserId());

        List<Cart> cartList = cartService.list(wrapper);

        List<Product> productList = productService.getListByCart(cartList);

        List<String> productDetails = new ArrayList<>();

        for (Product product:productList){
            String detail = product.getProductName()+product.getProductType()
                    +product.getProductColor()+product.getProductSize();
            productDetails.add(detail);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("users",user);
        map.put("cartNum",cartNum);
        map.put("productList",productList);
        map.put("prodetails",productDetails);
        map.put("cartList",cartList);
        System.out.println(map);

        System.out.println("1111返回return");

        return ServerResponse.success("ok",map);

    }

    //加入购物车
    @PostMapping("addToCart")
    public ServerResponse AddProductToCart(Cart cart,HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println("cart===================================="+cart);
        Integer userId =TokenUtil.parseToken(token).getCustId();
        Integer proId = cart.getProductId();
        Integer proNum = cart.getProductNum();
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localdatetime============"+localDateTime);
        Cart cart1 = new Cart();
        cart1.setUserId(userId);
        cart1.setProductNum(proNum);
        cart1.setProductId(proId);
        cart1.setPurchaseTime(localDateTime);
        cart1.setCartStatus(1);
        System.out.println(cart1);
        ServerResponse result = cartService.savecart(cart1);
        System.out.println("1111111111"+result);
        return result;
    }

}
